package net.hymnal.church;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.util.Log;

public class HymnsManager {
	public enum HYMNS_TYPE {
		TYPE_NONE, TYPE_COMMON, TYPE_YOUNG
	};

	private static HYMNS_TYPE selectedType;
	static ArrayList<SingleHymn> m_arrayCommonHymns;
	static ArrayList<SingleHymn> m_arrayYoungHymns;

	static final int mMax_common_hymns = 551;
	static final int mMax_young_hymns  = 180;
	
	static private int selectedCount;
	
	static public boolean isSelected() {
        return (selectedCount > 0);
	}
	
	static public void resetSelection() {
        selectedCount = 0;
	}
	
	
	static void selectHymnType(HYMNS_TYPE type){
		selectedType = type;
	}
	
	

	static void readYoungHymns(Activity a) {
		HymnsManager.m_arrayYoungHymns = new ArrayList<SingleHymn>();
		String fileName = "young/";
		for (int i = 0; i < mMax_young_hymns; i++) {
			String appendStr = "";

			if (i < 100) {
				appendStr += "0";
			}

			if (i < 10) {
				appendStr += "0";
			}

			String fullName = fileName + appendStr + Integer.toString(i)
					+ ".txt";
			try {
				final InputStream is = a.getResources().getAssets()
						.open(fullName);
				if (is != null) {
					byte b_buffer[] = null;
					int fileSize = is.available();
					b_buffer = new byte[fileSize + 1];
					is.read(b_buffer);
					String text = new String(b_buffer, "Cp1251");
					SingleHymn singleHymn = new SingleHymn(i, text);
					HymnsManager.m_arrayYoungHymns.add(singleHymn);
				}
			} catch (IOException e) {
				Log.e("IO", "Young:File not found:" + i);
			}
		}
	}

	static void readCommonHymns(Activity a) {
		HymnsManager.m_arrayCommonHymns = new ArrayList<SingleHymn>();
		String FileName = "common/r";
		for (int i = 0; i < mMax_common_hymns; i++) {
			String fullName = FileName + Integer.toString(i) + ".txt";
			try {
				final InputStream is = a.getResources().getAssets()
						.open(fullName);
				if (is != null) {
					byte b_buffer[] = null;
					int fileSize = is.available();
					b_buffer = new byte[fileSize + 1];
					is.read(b_buffer);
					String text = new String(b_buffer, "Cp1251");
					SingleHymn singleHymn = new SingleHymn(i, text);
					HymnsManager.m_arrayCommonHymns.add(singleHymn);
				}
			} catch (IOException e) {
				Log.e("IO", "Common:File not found:" + i);
			}
		}
	}

	static int getNumberHymns() {
		if (selectedType == HYMNS_TYPE.TYPE_COMMON) {
			return HymnsManager.m_arrayCommonHymns.size();
		}
		if (selectedType == HymnsManager.HYMNS_TYPE.TYPE_YOUNG) {
			return HymnsManager.m_arrayYoungHymns.size();
		}
		return 0;
	}

	static SingleHymn getSingleHymn(int index) {
		if (HymnsManager.selectedType == HYMNS_TYPE.TYPE_COMMON) {
			return m_arrayCommonHymns.get(index);
		}
		if (HymnsManager.selectedType == HYMNS_TYPE.TYPE_YOUNG) {
			return m_arrayYoungHymns.get(index);
		}
		return null;
	}

	static boolean SelectHymns(String subString) {		
		selectedCount = 0;			
		int num = getNumberHymns();
		for (int i = 0; i < num; i++) {
			if (selectedType == HYMNS_TYPE.TYPE_COMMON) {
				SingleHymn h = m_arrayCommonHymns.get(i);
				h.checkTextAndSelect(subString);
				if (h.isSelected()) {
					selectedCount++;
				}
				continue;
			}
			if (selectedType == HYMNS_TYPE.TYPE_YOUNG) {
				SingleHymn h = m_arrayYoungHymns.get(i);
				h.checkTextAndSelect(subString);
				if (h.isSelected()) {
					selectedCount++;
				}
			}
			if (selectedType == HYMNS_TYPE.TYPE_NONE) {
				return false;
			}			
		}
		return (selectedCount > 0);		
	}
	
	static String[] getSelectedArray() {
		int k = 0;
		String[] array = new String[selectedCount];
		int num = getNumberHymns();
		for (int i = 0; i < num; i++) {
			if (selectedType == HYMNS_TYPE.TYPE_COMMON) {
				SingleHymn h = m_arrayCommonHymns.get(i);
				if (h.isSelected()) {
					array[k++] = Integer.toString(i + 1);
				}
				continue;
			}
			if (selectedType == HYMNS_TYPE.TYPE_YOUNG) {
				SingleHymn h = m_arrayYoungHymns.get(i);
				if (h.isSelected()) {
					array[k++] = Integer.toString(i + 1);
				}
			}
		}
		return array;
	}
}
